package peace.util
{
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.getTimer;
/**
 * 
 * Меряем фпс
 * 
 * @author peace
 * 
 */
public class FpsMeter extends TextField
{
	/**
	 * 
	 */
	private var frames:int = 0;
	/**
	 * 
	 */
	private var prevTime:int = 0;
	/**
	 * 
	 * 
	 */	
	public function FpsMeter()
	{
		super();
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStage, false,0,true);
		addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage, false,0,true);
		
		defaultTextFormat = new TextFormat("arial", 16, 0, true);
		autoSize = TextFieldAutoSize.LEFT;
		background = true;
//		border= true;
		mouseEnabled = false;
	}
	/**
	 * 
	 * @param e
	 * 
	 */
	private function onAddedToStage (e:Event):void
	{
		addEventListener(Event.ENTER_FRAME, onFrame);
	}
	/**
	 * 
	 * @param e
	 * 
	 */
	private function onRemovedFromStage (e:Event):void
	{
		removeEventListener(Event.ENTER_FRAME, onFrame);
	}
	/**
	 * 
	 * @param e
	 * 
	 */
	private function onFrame (e:Event):void
	{
		var t:int = getTimer();
		
		if (t - prevTime >= 1000)
		{
			text = frames+"";
			
			frames = 0;
			prevTime = t;
		}
		else
		{
			frames ++;
		}
	}
	
	
}
}

